#include <wx/wxprec.h>

#ifdef __BORLANDC__
	#pragma hdrstop
#endif

#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif
#include <wx/aboutdlg.h>
#include <wx/file.h>
#include <wx/filename.h>
#include <wx/wfstream.h>
#include <wx/datstrm.h>
#include <wx/textfile.h>
#include <wx/dynarray.h>
#include "myapp.h"
#include "kakeibo.h"

#include "kakeibomeister.xpm"

DECLARE_APP(MyApp);

WX_DECLARE_OBJARRAY(wxArrayString,wx2DArrayString);
#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY(wx2DArrayString);

void MyFrame::km_ReportPublish_clicked(wxCommandEvent &event)
{
	wxArrayString arrheader, arrfooter;
	wxArrayInt opt;
	for (size_t i = 0; i < km_ReportOptions->GetCount(); i++) {
		opt.Add(km_ReportOptions->IsChecked(i) ? 1 : 0);
	}
	for (int i = 0; i < WXSIZEOF(km_ReportHeader); i++) arrheader.Add(km_ReportHeader[i]->GetValue());
	for (int i = 0; i < WXSIZEOF(km_ReportFooter); i++) arrfooter.Add(km_ReportFooter[i]->GetValue());
	ReportPrintout *rp = new ReportPrintout(this,wxGetApp().AppDB.AppSetting.GetBookName());
	rp->SetReportMargin(pagesetup_data->GetMarginTopLeft().y, pagesetup_data->GetMarginTopLeft().x,
		pagesetup_data->GetMarginBottomRight().x, pagesetup_data->GetMarginBottomRight().y);
	rp->SetReportType(km_ReportSource->GetSelection());
	rp->SetHeaderString(arrheader);
	rp->SetFooterString(arrfooter);
	rp->SetHeaderFooterLine(km_ReportLineHeader->GetValue(),km_ReportLineFooter->GetValue());
	switch (km_ReportSource->GetSelection()) {
	case 0: //Day
		{
			rp->Bind_DB_day(dbbudget.begin(),dbbudget.end());
			rp->Bind_DB_total(dbtotal);
			DB_reserved dbreserve;
			int budgetdiff = 0;
			int cnt = wxGetApp().AppDB.AppSetting.GetBudgetCount();
			for (int i = 0; i < cnt; i++) {
				int diff;
				if (CheckBudgetDate(dbbudget.at(0).GetDate(),dbbudget.at(0).GetHandling(),wxGetApp().AppDB.AppSetting.GetBudget(i),&diff)) {
					dbreserve = wxGetApp().AppDB.AppSetting.GetBudget(i);
					budgetdiff += dbreserve.GetValue() + diff;
					break;
				}
			}
			rp->Bind_DB_reserved(dbreserve,budgetdiff);
		}
		break;
	case 1: //Week
		{
			wxDateTime first = km_Calender->GetDate();
			first.SetToPrevWeekDay(wxDateTime::Sun);
			wxDateTime last = km_Calender->GetDate();
			last.SetToNextWeekDay(wxDateTime::Sat);
			//first.Subtract(wxDateSpan(0,0,2,0));
			//last.Subtract(wxDateSpan(0,0,2,0));

			DB_total totdb, restot;
			totdb.SetHandling(curhandling);
			totdb.SetDate(first);
			restot = wxGetApp().AppDB.GetTotalNear(totdb,_T("<="));
			wxString sql = StrF(WXSQL_FOR_DATERANGE,
				first.Format(DATEFORM).c_str(),last.Format(DATEFORM).c_str(),curhandling);
			wxSQLite3ResultSet rset = wxGetApp().SQLDB().ExecuteQuery(sql);
			DBL_WeeklyReport weekdata;
			while (rset.NextRow()) {
				DB_WeeklyReport db;
				db.SetExpenseName(rset.GetString(0));
				db.SetItemName(rset.GetString(1));
				db.SetItemDate(0,first);
				db.SetAmount(0,rset.GetInt(2));
				db.SetIO(0,(InoutType)rset.GetInt(3));
				weekdata.Add(db);
			}
			//sql = _T("select count(*) from (select ex_name, it_name from db_expense left join db_items on ex_id = it_expense group by ex_id, it_id order by ex_io);");
			rp->Bind_DB_total(restot);
			rp->Bind_DB_weeklymonthly(weekdata);
		}
		break;
	case 2: //Month
		{
			wxDateTime first = km_Calender->GetDate();
			first.SetDay(1);
			wxDateTime last = km_Calender->GetDate().GetLastMonthDay();
			//first.Subtract(wxDateSpan(0,2,0,0));
			//last.Subtract(wxDateSpan(0,2,0,0));

			DB_total totdb, restot;
			totdb.SetHandling(curhandling);
			totdb.SetDate(first);
			restot = wxGetApp().AppDB.GetTotalNear(totdb,_T("<="));
			wxString sql = StrF(WXSQL_FOR_DATERANGE,
				first.Format(DATEFORM).c_str(),last.Format(DATEFORM).c_str(),curhandling);
			wxSQLite3ResultSet rset = wxGetApp().SQLDB().ExecuteQuery(sql);
			DBL_WeeklyReport weekdata;
			while (rset.NextRow()) {
				DB_WeeklyReport db;
				db.SetExpenseName(rset.GetString(0));
				db.SetItemName(rset.GetString(1));
				db.SetItemDate(0,first);
				db.SetAmount(0,rset.GetInt(2));
				db.SetIO(0,(InoutType)rset.GetInt(3));
				weekdata.Add(db);
			}
			rp->Bind_DB_total(restot);
			rp->Bind_DB_weeklymonthly(weekdata);

		}
		break;
	case 3: //Searches
		{
			rp->Bind_DB_day(dbsearch.begin(),dbsearch.end());
			rp->SetSearchConditions(km_SerCondList->GetStrings());
		}
		break;
	}
	if (km_ReportTitle->IsEmpty()) {
		rp->SetReportTitle(wxGetApp().AppDB.AppSetting.GetBookName());
	}else{
		rp->SetReportTitle(km_ReportTitle->GetValue());
	}
	rp->SetOptions(opt);
	ReportPrintout *rp2 = new ReportPrintout(this,wxGetApp().AppDB.AppSetting.GetBookName());
	
	*rp2 = *rp;
	wxPrintDialogData pdata(*print_data);
	wxPrintPreview *pp = new wxPrintPreview(rp,rp2,&pdata);
	if (!pp->Ok()) {
		delete pp;
		Msgbox(_T("do not hava print preview."),this);
		return;
	}
	//---show preview dialog
	wxPreviewFrame *pf = new wxPreviewFrame(pp,this,_T("test"),wxDefaultPosition,wxSize(640,480));
	pf->Center();
	pf->Initialize();
	pf->Show();

	/*rep_dialog->Bind_PageData(&pageInfo);
	rep_dialog->Bind_Setup(wxGetApp().AppDB.AppSetting);
	if (rep_dialog->ShowModal()) {

	}*/
}
